<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "user".
 *
 * @property integer $acc_id
 * @property string $full_name
 * @property string $birthday
 * @property string $address
 * @property string $phone
 * @property string $username
 * @property string $password
 * @property integer $acc_type
 *
 * @property AccPayAddress[] $accPayAddresses
 * @property AccProject[] $accProjects
 */
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
	
    public $authKey;
    public $accessToken;
	public $password_confirm;
	public $input_old_pass;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['full_name', 'birthday', 'address', 'email', 'username', 'password'], 'required'],
            [['input_old_pass'], 'required', 'on' => 'update'],
            [['password_confirm'], 'required', 'on' => ['create', 'update']],
            [['full_name', 'address', 'email'], 'string'],
            [['birthday'], 'safe'],
            [['acc_type', 'phone'], 'integer'],
            [['username'], 'string', 'max' => 11],
            [['password', 'password_confirm'], 'string', 'max' => 200],
            [['password_confirm'], 'compare', 'compareAttribute' => 'password', 'message' => 'Mật khẩu mới không trùng khớp', 'on' => ['create', 'update']],
            [['input_old_pass'], 'validateOldPass', 'on' => 'update'],
            [['email'], 'email']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'acc_id' => 'Acc ID',
            'full_name' => 'Họ tên',
            'birthday' => 'Ngày sinh',
            'address' => 'Địa chỉ',
            'email' => 'Địa chỉ email',
            'phone' => 'Số điện thoại',
            'username' => 'Tên tài khoản',
            'password' => 'Mật khẩu',
            'password_confirm' => 'Nhập lại mật khẩu',
            'input_old_pass' => 'Mật khẩu hiện tại',
            'acc_type' => 'Loại tài khoản',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAccPayAddresses()
    {
        return $this->hasMany(AccPayAddress::className(), ['acc_id' => 'acc_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAccProjects()
    {
        return $this->hasMany(AccProject::className(), ['acc_id' => 'acc_id']);
    }
///////////////////////////////////login
	/**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return (User::find()->where(['acc_id' => $id])->one())!==null ? new static(User::find()->where(['acc_id' => $id])->one()) : null;
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        foreach (self::$users as $user) {
            if ($user['accessToken'] === $token) {
                return new static($user);
            }
        }

        return null;
    }

    /**
     * Finds user by username
     *
     * @param  string      $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        if (($user = User::find()->where(['username' => $username])->one()) !== null) {
            return new static($user);
        } else {
            return null;
        }
    }

    /**
     * Validates password
     *
     * @param  string  $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password)
    {
        return $this->password === $password;
    }
	
	 /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->acc_id;
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->authKey;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return $this->authKey === $authKey;
    }	
	
	//kiểm tra nhập lại mật khẩu cũ khi có đổi mật khẩu
	public function validateOldPass($attribute, $params)
	{
		if (!$this->hasErrors()) 
		{
			if(md5($this->$attribute) != Yii::$app->user->identity->password)
			{
			  $this->addError($attribute, 'Mật khẩu sai');
			}
		}
	}
}